<?php
/**
 * System data objects.
 *
 * @author Lei Lee
 */
class SystemData {
	/**
	 * Store POST data collection.
	 *
	 * @var array
	 */
	private $_post = array();
	
	/**
	 * Store GET data collection.
	 *
	 * @var array
	 */
	private $_gets = array();
	
	/**
	 * Store $_SERVER data collection.
	 *
	 * @var array
	 */
	private $_envs = array();
	
	/**
	 * Remove both ends of the string is a space character?
	 *
	 * @var boolean
	 */
	private $_is_trim = false;
	
	/**
	 * Constructor.
	 *
	 */
	function __construct() {
		if (get_magic_quotes_runtime()) {
			$this->deslashes($_POST);
			$this->deslashes($_GET);
		}
		
		$this->_post = &$_POST;
		$this->_gets = &$_GET;
		$this->_envs = &$_SERVER;
	}
	
	/**
	 * Access GET parameters.
	 *
	 * @param string $key
	 * @param mixed $default_value
	 */
	function gets($key, $default_value = NULL) {
		if (isset($this->_gets[$key]))
			return $this->_is_trim ? trim($this->_gets[$key]) : $this->_gets[$key];
		else
			return $default_value;
	}
	
	/**
	 * Access POST parameters.
	 *
	 * @param string $key
	 * @param mixed $default_value
	 */
	function post($key, $default_value = NULL) {
		if (isset($this->_post[$key]))
			return $this->_is_trim ? trim($this->_post[$key]) : $this->_post[$key];
		else
			return $default_value;
	}
	
	/**
	 * Access $_SERVER parameters.
	 *
	 * @param string $key
	 */
	function envs($key) {
		return $this->_envs[$key];
	}
	
	/**
	 * Remove the quotes escaped.
	 *
	 * @param array $data
	 */
	function deslashes(&$data) {
		if (is_array($data)) {
			foreach ( $data as $key => $value ) {
				if (is_array($value))
					$this->deslashes($data[$key]);
				elseif (is_string($value))
					$data[$key] = stripslashes($value);
			}
		} elseif (is_string($data)) {
			$data = stripslashes($data);
		}
	}
	
	/**
	 * Chinese and English mixed string interception.
	 * 
	 * @param string $string
	 * @param int $length
	 * @param string $dot
	 * @param string $charset
	 * @return string
	 */
	function truncate($string, $length, $dot = '...', $charset = 'gbk') {
		
		if (strlen($string) <= $length) {
			return $string;
		}
		
		$string = str_replace(array(
			'　', 
			'&nbsp;', 
			'&amp;', 
			'&quot;', 
			'&lt;', 
			'&gt;' 
		), array(
			'', 
			'', 
			'&', 
			'"', 
			'<', 
			'>' 
		), $string);
		
		$strcut = '';
		if (strtolower($charset) == 'utf-8') {
			$n = $tn = $noc = 0;
			while ( $n < strlen($string) ) {
				$t = ord($string[$n]);
				if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
					$tn = 1;
					$n++;
					$noc++;
				} elseif (194 <= $t && $t <= 223) {
					$tn = 2;
					$n += 2;
					$noc += 2;
				} elseif (224 <= $t && $t < 239) {
					$tn = 3;
					$n += 3;
					$noc += 2;
				} elseif (240 <= $t && $t <= 247) {
					$tn = 4;
					$n += 4;
					$noc += 2;
				} elseif (248 <= $t && $t <= 251) {
					$tn = 5;
					$n += 5;
					$noc += 2;
				} elseif ($t == 252 || $t == 253) {
					$tn = 6;
					$n += 6;
					$noc += 2;
				} else {
					$n++;
				}
				
				if ($noc >= $length) {
					break;
				}
			
			}
			if ($noc > $length) {
				$n -= $tn;
			}
			
			$strcut = substr($string, 0, $n);
		
		} else {
			for($i = 0; $i < $length; $i++) {
				$strcut .= ord($string[$i]) > 127 ? $string[$i] . $string[++$i] : $string[$i];
			}
		}
		
		return $strcut . $dot;
	}
	
	/**
	 * Generate a unique identification string.
	 *
	 * @param string $prefix
	 * @return string
	 */
	function unique($prefix = '') {
		$s = uniqid($prefix, true);
		$s = str_replace('.', '_', $s);
		return $s;
	}
	
	/**
	 * Data character set encoding conversion.
	 *
	 * @param array|string $data
	 * @param string $from
	 * @param string $to
	 */
	function encoding(&$data, $from = 'GBK', $to = 'UTF-8') {
		if (is_string($data)) {
			$data = iconv($from, $to, $data);
		} elseif (is_array($data) && !empty($data)) {
			foreach ( $data as $key => $value ) {
				if (is_array($value)) {
					$this->encoding($data[$key], $from, $to);
				} elseif (is_string($value)) {
					$data[$key] = iconv($from, $to, $value);
				}
			}
		}
	}
	
	/**
	 * Encrypted data and return Base64-encoded string.
	 *
	 * @param string $s
	 * @param string $secure_key
	 * @return string
	 */
	function encrypt($s, $secure_key) {
		if (null == $s || !is_string($s))
			return false;
		
		$td = mcrypt_module_open('tripledes', '', 'ecb', '');
		$td_size = mcrypt_enc_get_iv_size($td);
		$iv = mcrypt_create_iv($td_size, MCRYPT_RAND);
		$key = substr($secure_key, 0, $td_size);
		mcrypt_generic_init($td, $key, $iv);
		$ret = base64_encode(mcrypt_generic($td, $s));
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		
		return $ret;
	}
	
	/**
	 * Decrypt the data.
	 *
	 * @param string $s
	 * @param string $secure_key
	 * @return string
	 */
	function decrypt($s, $secure_key) {
		if (null == $s)
			return false;
		
		$td = mcrypt_module_open('tripledes', '', 'ecb', '');
		$td_size = mcrypt_enc_get_iv_size($td);
		$iv = mcrypt_create_iv($td_size, MCRYPT_RAND);
		$key = substr($secure_key, 0, $td_size);
		mcrypt_generic_init($td, $key, $iv);
		$ret = trim(mdecrypt_generic($td, base64_decode($s)));
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		
		return $ret;
	}
	
	/**
	 * Set the POST / GET values both ends of the string to remove the space character.
	 *
	 */
	function isTrim() {
		$this->_is_trim = true;
	}
}